/****************
结果评审
* */
<template>
  <basic-container>
    <el-row>
      <targetExaminHead
        :sign="{ a: false, b: false, c:false ,d:false ,e: false,f:false,g:false,h:true,}"
      />
    </el-row>
    <el-row v-if="isEdit==1">
      <el-row style="margin:25px 0 0 0 ;">
        <div :height="searchheight">
          <span
            style="float:right;margin-top: -30px;cursor: pointer;"
            @click="searchchangeadvanced"
          >
            {{ searchadvanced ? "查询条件收起" : "查询条件展开" }}
            <i
              :class="searchadvanced ? 'el-icon-arrow-up' : 'el-icon-arrow-down'"
            />
          </span>
          <div v-show="searchadvanced">
            <el-form
              :inline="true"
              :model="searchForm"
              @keyup.enter.native="getDataList()"
              class="form-inline"
            >
              <el-form-item label="考核对象">
                <el-input v-model="searchForm.assessRealName" placeholder="考核对象" :clearable="true"></el-input>
              </el-form-item>
              <el-form-item label="考核名称">
                <el-input v-model="searchForm.name" placeholder="考核名称" :clearable="true"></el-input>
              </el-form-item>
              <el-form-item label="考核周期">
                <selectSearch @getDataByPerid="getDataByPerid"></selectSearch>
              </el-form-item>
              <el-form-item>
                <el-button
                  icon="el-icon-search"
                  type="primary"
                  class="marginLeft"
                  @click="getDataList(1)"
                >检索</el-button>
              </el-form-item>
            </el-form>
          </div>
        </div>
      </el-row>
      <div style="height: 10px; border-bottom: 1px solid #b0bde2"></div>
      <el-row style="margin:10px 0 0 0">
        <div class="btnBox">
          <div style="margin:20px 0 0 0 ">
            <span>指标考核&nbsp;&nbsp;</span>
            <el-radio-group v-model="choosePerOrDept" @change="choosePerOrDeptChange">
              <el-radio :label="0">人员</el-radio>
              <el-radio :label="1">机构</el-radio>
            </el-radio-group>
          </div>
          <div>
            <!-- <el-button type="success" @click="passTargetExamin">通过</el-button> -->
            <el-button type="primary" @click="exportData">导出</el-button>
            <el-button type="primary" @click="exportDataZb">导出指标</el-button>
            <el-button type="primary" @click="batchFilling">批量填报</el-button>
            <el-button plain type="primary" @click="submit(0)" :disabled="isAgreeOrNot">同意</el-button>
            <el-button plain type="primary" @click="submit(1)" :disabled="isAgreeOrNot">驳回</el-button>
            <!--<el-button type="primary" @click="passTargetExamin">批量提交</el-button>-->
          </div>
        </div>
      </el-row>
      <el-row>
        <el-table
          style="width: 100%;margin-top:20px"
          :data="dataList"
          border
          stripe
          ref="tableRef"
          header-align="center"
          v-loading="dataListLoading"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" align="center" header-align="center" width="55"></el-table-column>
          <el-table-column type="index" align="center" label="序号" width="55"></el-table-column>
          <el-table-column
            prop="assessUserName"
            align="center"
            label="员工编号"
            show-overflow-tooltip
            v-if="!choosePerOrDept"
          ></el-table-column>
          <el-table-column
            prop="assessRealName"
            align="center"
            label="考核对象"
            show-overflow-tooltip
            v-if="!choosePerOrDept"
          >
            <template slot-scope="scope">
              <span
                @click="handleEdit(scope.row)"
                style="color:rgb(14, 37, 140);cursor:pointer"
              >{{scope.row.assessRealName}}</span>
            </template>
          </el-table-column>

          <el-table-column
            prop="assessDeptName"
            align="center"
            label="考核对象"
            show-overflow-tooltip
            v-else
          >
            <template slot-scope="scope">
              <span
                @click="handleEdit(scope.row)"
                style="color:rgb(14, 37, 140);cursor:pointer"
              >{{scope.row.assessDeptName}}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="assessDeptName"
            align="center"
            label="部门"
            show-overflow-tooltip
            v-if="!choosePerOrDept"
          ></el-table-column>
          <el-table-column prop="itemNum" align="center" label="指标数量" show-overflow-tooltip></el-table-column>
          <el-table-column prop="name" align="center" label="考核名称" show-overflow-tooltip></el-table-column>
          <el-table-column prop="period" align="center" label="考核周期" show-overflow-tooltip></el-table-column>
          <el-table-column prop="sumScore" align="center" label="总得分" show-overflow-tooltip></el-table-column>

          <el-table-column prop="state" align="center" label="状态" show-overflow-tooltip>
            <template slot-scope="scope">
              <span>{{scope.row.state | transTargetState}}</span>
            </template>
          </el-table-column>
          <!-- <el-table-column align="center" fixed="right" min-width="120" label="操作">
            <template slot-scope="scope">
              <el-button type="primary" plain size="small" @click="handleEdit(scope.row)">确认</el-button>
            </template>
          </el-table-column>-->
        </el-table>
        <!--分页-->
        <div class="avue-crud__pagination">
          <el-pagination
            @size-change="sizeChangeHandle"
            @current-change="currentChangeHandle"
            :current-page="pageData.pageIndex"
            :page-sizes="[10, 20, 50, 100]"
            :page-size="pageData.pageSize"
            :total="pageData.totalPage"
            background
            layout="total, sizes, prev, pager, next, jumper"
          ></el-pagination>
        </div>
      </el-row>
    </el-row>
    <el-row v-if="isEdit==2">
      <el-table
        style="width: 100%;margin-top:20px"
        :data="detailDataList"
        border
        v-if="isEdit==2"
        show-summary
        stripe
        header-align="center"
        v-loading="dataListLoading"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="index" align="center" label="序号" width="55"></el-table-column>
        <el-table-column prop="assessUserName" align="center" label="员工编号" show-overflow-tooltip></el-table-column>
        <el-table-column prop="assessRealName" align="center" label="姓名" show-overflow-tooltip></el-table-column>
        <el-table-column prop="assessDeptName" align="center" label="部门" show-overflow-tooltip></el-table-column>
        <el-table-column prop="type" align="center" label="指标类别" show-overflow-tooltip></el-table-column>
        <el-table-column prop="name" align="center" label="指标名称" show-overflow-tooltip></el-table-column>
        <el-table-column prop="weight" align="center" label="权重" show-overflow-tooltip></el-table-column>
        <el-table-column prop="formula" align="center" label="计算公式" show-overflow-tooltip></el-table-column>
        <el-table-column prop="targetValue" align="center" label="目标值" show-overflow-tooltip></el-table-column>
        <el-table-column prop="unit" align="center" label="单位" show-overflow-tooltip></el-table-column>
        <el-table-column prop="finishValue" align="center" label="填报值" show-overflow-tooltip></el-table-column>
        <el-table-column prop="score" align="center" label="得分" show-overflow-tooltip></el-table-column>
        <el-table-column prop="finalScore" align="center" label="修改得分" show-overflow-tooltip>
          <template slot-scope="scope">
            <el-input
              @change="((val)=>{
              detailScoreChange(scope.row)})"
              v-model.number="scope.row.finalScore"
            ></el-input>
          </template>
        </el-table-column>
        <el-table-column prop="provideRealName" align="center" label="数据提供部门" show-overflow-tooltip></el-table-column>
      </el-table>
      <el-row style="margin:10px;text-align:right">
        <el-button type="success" @click="isEdit=1">确定</el-button>
        <el-button type="primary" @click="isEdit=1">返回</el-button>
      </el-row>
    </el-row>
    <el-row v-if="isEdit==3">
      <TianBaoTable
        v-if="isEdit == 3"
        ref="tianbaotable"
        :tianBaoTable="tianBaoTable"
        :ids="ids"
        :radio="radio"
        @fnBackOldPage="fnBackOldPage"
        :ndids="ndids"
      ></TianBaoTable>
    </el-row>
    <examinView v-if="examinVisible" ref="examinNode" @closeDialog="closeDialog1" @refresh="refresh"/>
  </basic-container>
</template>

<script>
import targetExaminHead from "@/views/dailyExamine/components/targetExaminHead"; //状态头
import {
  resultExaminList,
  resultExaminDetail,
  resultExaminPass,
  resultExaminDetailSave
} from "@/api/dailyExamine/targetExamine/InitiateFilling";
import {
  examinPeriodList,
  scoreExportExcel,
  exportExcelZb
} from "@/api/dailyExamine/targetExamine/createAssessment";
import TianBaoTable from "./htndhuizong-table.vue";
import { examineList } from "@/api/dailyExamine/targetExamine/createAssessment"; //接口api
import { processResultAudit } from "@/api/dailyExamine/targetExamine/InitiateFilling";
import examinView from "./examinView.vue";
import selectSearch from "@/views/dailyExamine/components/select-search.vue"; //流程组件

export default {
  data() {
    return {
      examinVisible: false, //审核弹窗显隐
      isAgreeOrNot: true,
      choosePerOrDept: 0, //选择的是人员还是机构
      tianBaoTable: {},
      ids: [],
      radio: "1",
      ndids: [],
      isPlanSummary: false,
      activeName: "first",
      detailDataList: [], //修改页面的数组
      periodValue: null, //考核周期绑定值
      isEdit: 1, //是否修改
      searchheight: "0", //搜索组件的高度
      searchadvanced: true, //搜索的显隐
      searchForm: {}, //搜索对象
      addVisible: false, //
      dataListLoading: false, //表格是否加载
      dataList: [], //表格数组
      multipleSelection: [], //勾选数据
      pageData: {
        //分页对象
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0
      },
      periodList: []
    };
  },
  components: {
    examinView,
    TianBaoTable,
    targetExaminHead ,//状态头
    selectSearch//搜索栏目下拉组件
  },
  watch: {
    multipleSelection: {
      handler(val) {
        if (val.length == 0) {
          this.isAgreeOrNot = true;
        } else {
          this.isAgreeOrNot = false;
        }
      }
    }
  },
  filters: {},
  created() {
    this.getDataList();
    this.getPeriodData();
  },
  methods: {
    //  人员or机构选择改变
    choosePerOrDeptChange(val) {
      this.searchForm = {};
      this.dataList = [];
      this.getDataList();
    },
    fnBackOldPage(data) {
      this.isEdit = 1;
      this.ids = [];
      this.multipleSelection = [];
      this.getDataList();
    },
    // 批量填报
    batchFilling() {
      if (this.multipleSelection.length == 0) {
        this.$message.warning("请先选择数据");
      } else {
        this.isEdit = 3;
        this.tianBaoTable = {
          lastPage: "zbnd"
        };
      }
    },
    // 获取考核周期数据
    getPeriodData() {
      examinPeriodList()
        .then(r => {
          if (r.data.code == 0) {
            this.periodList = r.data.data;
          }
        })
        .catch(err => {});
    },
    // 提交
    submit(type) {
      if (this.multipleSelection.length == 0) {
        this.$message.warning("请先选择要提交的数据");
      } else {
        var rows = this.$refs.tableRef.selection;
        if (rows.length == 0) {
          rows = this.$refs.tableRef.data;
        }

        this.$confirm(`确定批量${type == 0 ? "同意" : "驳回"}吗?`, "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(res => {
            console.log("同意or驳回");
            //提交
            if (type == 0) {
              var params = {
                rows: rows,
                type: 0
              };
              processResultAudit(params)
                .then(res => {
                  this.$message.closeAll();
                  this.$message({
                    type: "success",
                    message: "提交成功!"
                  });
                  this.getDataList();
                })
                .catch(() => {
                  this.$message({
                    type: "error",
                    message: "提交失败!"
                  });
                  this.getDataList();
                });
            }
            //驳回
            if (type == 1) {
              var params = {
                rows: rows,
                type: 1
              };
              processResultAudit(params)
                .then(res => {
                  this.$message({
                    type: "success",
                    message: "驳回成功!"
                  });
                  this.getDataList();
                })
                .catch(() => {
                  this.$message({
                    type: "error",
                    message: "提交失败!"
                  });
                  this.getDataList();
                });
            }
          })
          .catch(() => {
            console.log("取消");
            this.$message({
              type: "info",
              message: "已取消"
            });
          });
      }
    },
    // 得分修改 提交数据
    detailScoreChange(data) {
      resultExaminDetailSave(data.id, data.finalScore)
        .then(r => {
          if (r.data.code == 0) {
            this.$message.success("修改成功");
          } else {
            this.$message.error(r.data.msg);
          }
        })
        .catch(err => {});
    },
    // 通过
    passTargetExamin() {
      if (this.multipleSelection.length == 0) {
        this.$message.warning("请先选择数据");
      } else {
        let ids = this.multipleSelection.map(it => {
          return it.id;
        });
        resultExaminPass(ids.toString())
          .then(r => {
            if (r.data.code == 0) {
              this.$message.success("审核成功");
              this.getDataList();
            } else {
              this.$message.error(r.data.msg);
            }
          })
          .catch(err => {});
      }
    },
    // 关闭审核弹窗
    closeDialog1() {
      this.examinVisible = false;
      this.getDataList();
    },
    //   修改
    handleEdit(data) {
      this.examinVisible = true;
      this.$nextTick(() => {
        this.$refs.examinNode.init(data);
      });
    },
    // 导出
    exportData() {
      var data = this.multipleSelection;
      if (data.length == 0) {
        this.$message.closeAll();
        this.$message.warning("请选择一个工作！");
        return;
      }
      var filename = "结果评审导出" + ".xlsx";
      var ids = [];
      for (var i = 0; i < data.length; i++) {
        ids.push(data[i].id);
      }
      scoreExportExcel({
        ids: ids
      }).then(response => {
        this.$message.closeAll();
        this.$message.success("导出成功!");
        let url = window.URL.createObjectURL(new Blob([response.data]));
        let link = document.createElement("a");
        link.style.display = "none";
        link.href = url;
        link.setAttribute("download", filename);
        document.body.appendChild(link);
        link.click();
      });
    },
    exportDataZb(){
      var data = this.multipleSelection;
      if (data.length == 0) {
        this.$message.closeAll();
        this.$message.warning("请选择一个工作！");
        return;
      }
      var filename = "结果评审指标导出" + ".xlsx";
      var ids = [];
      for (var i = 0; i < data.length; i++) {
        ids.push(data[i].id);
      }
      exportExcelZb({
        ids: ids
      }).then(response => {
        this.$message.closeAll();
        this.$message.success("导出成功!");
        let url = window.URL.createObjectURL(new Blob([response.data]));
        let link = document.createElement("a");
        link.style.display = "none";
        link.href = url;
        link.setAttribute("download", filename);
        document.body.appendChild(link);
        link.click();
      });
    },
    // 查询条件的展开关闭
    searchchangeadvanced() {
      this.searchadvanced = !this.searchadvanced;
      if (this.searchadvanced) {
        this.searchheight = "auto";
      }
    },
    // 查询列表
    getDataList(current) {
      this.dataListLoading = true;
      examineList(
        Object.assign(
          {
            current: current ? current : this.pageData.pageIndex,
            size: this.pageData.pageSize,
            type: this.choosePerOrDept
          },
          this.searchForm
        ),
        8
      )
        .then(r => {
          if (r.data.code == 0) {
            this.dataListLoading = false;
            this.dataList = r.data.data.records;
            this.pageData.totalPage = r.data.data.total;
          }
        })
        .catch(err => {});
    },
    // 勾选数据
    handleSelectionChange(val) {
      this.multipleSelection = val;
      this.ids = val.map(it => {
        return it.id;
      });
      if (val[0].type == 0) {
        this.radio = 1;
      } else {
        this.radio = 2;
      }
    },
    // 条数变化
    sizeChangeHandle(val) {
      this.pageData.pageSize = val;
      this.getDataList();
    },
    // 页码分页变化
    currentChangeHandle(val) {
      this.pageData.pageIndex = val;
      this.getDataList();
    },
    refresh(){//刷新
      this.getDataList();
    },
      //选择周期执行搜索功能
      getDataByPerid(period){
          this.searchForm.period = period ;
          this.getDataList() ;
      }
  }
};
</script>

<style lang="scss" scoped>
::v-deep .el-select .el-select__tags {
  overflow: auto !important;
}
::v-deep.el-tag.el-tag--info {
  margin: 5px;
  border: 1px solid green;
}
.btnBox {
  width: 100%;
  display: flex;
  justify-content: space-between;

  > .mytabBox {
    flex: 1;
  }
}
</style>
